package com.example.xinyun.slice;

import com.example.xinyun.MyApplication;
import com.example.xinyun.ResourceTable;
import com.example.xinyun.domain.UrlContant;
import com.example.xinyun.db.User;
import com.example.xinyun.db.db_User;
import com.example.xinyun.domain.DataBase;
import com.example.xinyun.domain.MD5Util;
import com.google.gson.Gson;
import com.zzrv5.mylibrary.ZZRCallBack;
import com.zzrv5.mylibrary.ZZRHttp;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.TextField;
import ohos.agp.window.dialog.ToastDialog;

public class registerSlice extends AbilitySlice {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_register);
        TextField username=(TextField)findComponentById(ResourceTable.Id_reg_user);
        TextField phones=(TextField)findComponentById(ResourceTable.Id_phones);
        TextField password=(TextField)findComponentById(ResourceTable.Id_reg_password);
        TextField password2=(TextField)findComponentById(ResourceTable.Id_reg_password2);
        Button reg_btn=(Button) findComponentById(ResourceTable.Id_btn_reg);
        reg_btn.setClickedListener(new Component.ClickedListener() {
            @Override
            public void onClick(Component component) {
                if(username.getText().length()<6||username.getText().length()>10||phones.getText().length()!=11||password.getText().length()<8||password.getText().length()>16)
                {
                    new ToastDialog(registerSlice.this)
                            .setText("输入不合法")
                            .show();
                }else {
                    if (!password.getText().equals(password2.getText()))
                    {
                        new ToastDialog(registerSlice.this)
                                .setText("两次输入的密码不同")
                                .show();
                    }else {
                        ZZRHttp.postJson(UrlContant.URL+"selectUser", "{\"user\":\"" + username.getText() + "\"}", new ZZRCallBack.CallBackString() {
                            @Override
                            public void onFailure(int i, String s) {
                                new ToastDialog(registerSlice.this)
                                        .setText("网络连接失败，请检查网络连接")
                                        .show();
                            }

                            @Override
                            public void onResponse(String s) {
                                db_User db_user = new Gson().fromJson(s, db_User.class);
                                if (db_user.getData()==null)
                                {
                                    User user=new User(username.getText(),phones.getText(), MD5Util.getMD5Str(password.getText()),MyApplication.user_Image);
                                    ZZRHttp.postJson(UrlContant.URL+"addUser", user.adduser(), new CallBackString() {
                                        @Override
                                        public void onFailure(int i, String s) {
                                            new ToastDialog(registerSlice.this)
                                                    .setText("网络连接失败，请检查网络连接")
                                                    .show();
                                        }

                                        @Override
                                        public void onResponse(String s) {
                                            db_User db_user1 = new Gson().fromJson(s, db_User.class);
                                            if (db_user1.getCode().equals("200"))
                                            {
                                                DataBase.writeUser("username",username.getText());
                                                //writeUser("password",password.getText());
                                                MyApplication.username=user.getUser();
                                                //MyApplication.password=user.getPassword();
                                                Intent intent=new Intent();
                                                //页面跳转
                                                present(new HomeFraction(),intent);
                                            }
                                        }
                                    });
                                } else {
                                    new ToastDialog(registerSlice.this)
                                            .setText("用户名已存在")
                                            .show();
                                }
                            }
                        });
                    }
                }
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
